package com.jishuo.nontax.web;

import com.jishuo.base.config.NonTaxConfig;
import com.jishuo.base.util.GTimeUtil;
import com.jishuo.base.util.UniqId;
import com.jishuo.nontax.api.socket.SocketClientCore;
import com.jishuo.nontax.bean.message.efisc.SetRealBankBalanceDTO;
import com.jishuo.nontax.bean.vo.BankBalanceVO;
import com.jishuo.nontax.bfc.service.BankBalanceService;
import com.jishuo.nontax.bfc.service.TaskService;
import com.tongtech.org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * className:testController
 * package:gov.hebcz.nontax.web.action.test
 * Description:
 *
 * @Date:2023/11/3 16:58
 * @Author:高艳闯
 */
@Repository
@Controller
@RequestMapping("/moneyTest")
public class EpayFiscTest{
    private static Logger logger = Logger.getLogger(EpayFiscTest.class);
    @Resource
    private TaskService taskService;
    @Resource
    private NonTaxConfig nonTaxConfig;
    @Resource
    private BankBalanceService bankBalanceService;


    @RequestMapping(value = "/getBalanceTest", method = {RequestMethod.GET, RequestMethod.POST})
    public void getMoney2(HttpServletRequest request, HttpServletResponse response) {
        logger.info("测试查询余额接口");
        String accountCode = nonTaxConfig.account;
        int bankid = nonTaxConfig.bankid;
        SocketClientCore socket = new SocketClientCore();
        // 发送报文
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String nowDate = format.format(date);
        // 发送报文
        String content="<?xml version=\"1.0\" encoding=\"GB2312\"?><message><head>"+
                "<channel>ntox</channel>"+
                "<transcode>931002</transcode>"+
                "<transdate>"+ GTimeUtil.getDateTime().substring(0, 8)+"</transdate>"+
                "<transtime>"+GTimeUtil.getDateTime().substring(8, 14)+"</transtime>"+
                "<serialno>"+ UniqId.getInstanceWithLog().getUniqID()+"</serialno>"+
                "<oper></oper>"+
                "</head>"+
                "<body>"+
                "<acctno>"+accountCode+"</acctno>"+
                "<acctname></acctname>"+
                "</body>"+
                "</message>";

        String fund = null;
        try {
            logger.info("准备发送的报文"+content);
            fund = socket.Client(content);
            ArrayList<SetRealBankBalanceDTO> setBals = new ArrayList<>();
            SetRealBankBalanceDTO setDto = new SetRealBankBalanceDTO();
            setDto.setBankid(bankid);
            setDto.setAccountnum(accountCode);
            setDto.setAccountdate(nowDate);
            setDto.setFund(fund);
            setBals.add(setDto);
            BankBalanceVO bankBalanceVO = new BankBalanceVO();
            bankBalanceVO.setFund(new BigDecimal(fund));
            bankBalanceVO.setAccountcode(accountCode);
            bankBalanceVO.setAccountdate(nowDate);
//            bankBalanceVO.setBankid(String.valueOf(bankid));
            bankBalanceService.setBankBalance(bankBalanceVO);
            logger.info("收到查询余额报文结果"+fund);
        } catch (Exception e) {
            logger.info(e.toString());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/autoCheckTest", method = {RequestMethod.GET, RequestMethod.POST})
    public void testAutoCheck(HttpServletRequest request, HttpServletResponse response) {
        logger.info("测试自动对账接口");
            taskService.autocheck();
    }
}
